//
//  MJRefresh+Extension.swift
//

import MJRefresh

extension UIScrollView {
    
    func mj_headerRefresh(_ hander: @escaping () -> Void) {
        let headerRefresh = MJRefreshNormalHeader()
        headerRefresh.lastUpdatedTimeLabel?.isHidden = true
        headerRefresh.stateLabel?.isHidden = true
        headerRefresh.refreshingBlock = hander
        mj_header = headerRefresh
    }
    
    func mj_footerRefresh(_ hander: @escaping () -> Void) {
        let footerRefresh = MJRefreshBackStateFooter()
        footerRefresh.refreshingBlock = hander
        mj_footer = footerRefresh
    }
    
    func beginRefreshing() {
        mj_header?.beginRefreshing()
    }
    
    func endHeaderRefresh() {
        if let mjHeader = mj_header, mjHeader.isRefreshing {
            mj_header?.endRefreshing()
        }
    }
    
    func endFooterRefresh() {
        if let mjFooter = mj_footer, mjFooter.isRefreshing {
            mj_footer?.endRefreshing()
        }
    }
    
    var isHeaderRefresh: Bool {
        if let mjHeader = mj_header {
            return mjHeader.isRefreshing
        }
        return false
    }
    
    var isFooterRefresh: Bool {
        if let mjFooter = mj_footer {
            return mjFooter.isRefreshing
        }
        return false
    }
    
    func ignored(_ scrollViewContentInsetTop: CGFloat) {
        mj_header?.ignoredScrollViewContentInsetTop = scrollViewContentInsetTop
    }
}

